import Datastore from 'nedb-promises'
import {
  InsertData, DeleteData, UpdateData, SearchData,
  InsertDataSync, DeleteDataSync, UpdateDataSync, SearchDataSync,
  SelectDatabase,
} from '@/utils/db'

const getTodoList = (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查找失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('web-games', 'practice', 'TodoList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

const addTodoItem = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  const { text } = queryParams
  const _db = SelectDatabase('web-games', 'practice', 'TodoList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  const insertDataCallback = (searchResult: ResponseDataType) => {
    const { code = -1, total = -1 } = searchResult
    if (code > 0 && total < 1) {
      InsertData(_db, queryParams, resJSON, sendData)
    } else {
      resJSON.note = `待办${text}已存在`
      sendData(resJSON)
    }
  }

  // 判断字典是否存在
  SearchData(_db, { text }, resJSON, insertDataCallback)
}

const insertAnswer = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  const { id } = queryParams
  const _db = SelectDatabase('web-games', 'practice', 'answer')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    const updateDataCallback = (resJSON: any) => {
      UpdateData(_db, { id }, queryParams, resJSON, sendData)
    }
    SearchData(_db, { id }, resJSON, updateDataCallback)
  } else {
    InsertData(_db, queryParams, resJSON, sendData)
  }
}

const getAnswer = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查找失败',
    data: [],
    total: -1,
  }

  const { userId, title } = queryParams
  const _db = SelectDatabase('web-games', 'practice', 'answer')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, { userId, title }, resJSON, sendData)
}

const insertQuestionnaire = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  const { id } = queryParams
  const _db = SelectDatabase('web-games', 'practice', 'question')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    const updateDataCallback = (resJSON: any) => {
      UpdateData(_db, { id }, queryParams, resJSON, sendData)
    }
    SearchData(_db, { id }, resJSON, updateDataCallback)
  } else {
    InsertData(_db, queryParams, resJSON, sendData)
  }
}

const getQuestionnaireList = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查找失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('web-games', 'practice', 'question')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

const insertUser = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  const { userId } = queryParams
  const _db = SelectDatabase('web-games', 'practice', 'user')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  const insertDataCallback = (searchResult: ResponseDataType) => {
    const { code = -1, total = -1 } = searchResult
    if (code > 0 && total < 1) {
      InsertData(_db, queryParams, resJSON, sendData)
    } else {
      resJSON.note = `用户已存在`
      sendData(resJSON)
    }
  }

  /**
   * 这里应该查询是否有userId, 没有则添加，有则返回用户已存在
   */
  SearchData(_db, { userId }, resJSON, insertDataCallback)
}

const getUserList = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查找失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('web-games', 'practice', 'user')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  console.log('queryParams', queryParams)

  SearchData(_db, queryParams, resJSON, sendData)
}

/**
 * 留言板
 */
const insertNote = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  const { content, author } = queryParams

  const _db = SelectDatabase('web-games', 'practice', 'noteList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  const insertDataCallback = (searchResult: ResponseDataType) => {
    const { code = -1, total = -1 } = searchResult
    if (code > 0 && total < 1) {
      InsertData(_db, queryParams, resJSON, sendData)
    } else {
      resJSON.note = `留言已存在`
      sendData(resJSON)
    }
  }

  SearchData(_db, { content, author }, resJSON, insertDataCallback)
}

const getNoteList = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查找失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('web-games', 'practice', 'noteList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

const deleteNote = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '删除失败',
    data: [],
    total: -1,
  }

  const { id } = queryParams
  const _db = SelectDatabase('web-games', 'practice', 'noteList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    DeleteData(_db, queryParams, resJSON, sendData)
  } else {
    resJSON.note = 'id 不能为空'
    resolve(resJSON)
  }
}

const deleteAllNote = async (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '删除失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('web-games', 'practice', 'noteList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  DeleteData(_db, queryParams, resJSON, sendData)
}


export default [
  {
    path: '/webGames/todoList',
    func: getTodoList,
  },
  {
    path: '/webGames/addTodo',
    func: addTodoItem,
  },
  {
    path: '/webGames/insertAnswer',
    func: insertAnswer,
  },
  {
    path: '/webGames/getAnswer',
    func: getAnswer,
  },
  {
    path: '/webGames/insertQuestionnaire',
    func: insertQuestionnaire,
  },
  {
    path: '/webGames/getQuestionnaireList',
    func: getQuestionnaireList,
  },
  {
    path: '/webGames/insertUser',
    func: insertUser,
  },
  {
    path: '/webGames/getUserList',
    func: getUserList,
  },
  {
    path: '/webGames/insertNote',
    func: insertNote,
  },
  {
    path: '/webGames/getNoteList',
    func: getNoteList,
  },
  {
    path: '/webGames/deleteNote',
    func: deleteNote,
  },
  {
    path: '/webGames/deleteAllNote',
    func: deleteAllNote,
  },
]